App({
  onLaunch: function() {
    let that = this;
    // 展示本地存储能力

    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo
              // Bmob.User.upInfo(res.userInfo)


              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  login: function() {
    // 登录
    let that = this;

    return new Promise(function (resolve, reject){
      wx.login({
        success: function(res) {
          if (res.code) {
            // 发起网络请求
            wx.request({
              url: that.globalData.url + '/wxapp/login_simple',
              data: {
                code: res.code,
              },
              success: function(req) {
                console.log('连接服务器成功')
                console.log(req);
                that.globalData.data = req.data;
                that.globalData.openid = req.data.openid;
                that.globalData.teamPre = req.data.teamPre;
                console.log(that.globalData.teamPre)
                wx.setStorageSync('cookieKey', req.header['Set-Cookie']);

                if (!req.data.exists) {
                  console.log("不存在信息，需要登录")
                  // wx.navigateTo({
                  //   url: '../login/login',
                  // })
                } else {
                  console.log("存在信息，已经授权不要登录")
                  // wx.switchTab({
                  //   url: '../detail/detail',
                  //   fail: function () {
                  //     console.info("跳转到帖子页失败")
                  //   }
                  // })
                }
                resolve()

              },
              fail:function(){
                
              }

            })
            console.log(res);
          } else {
            console.log('登录失败！' + res.errMsg)
          }
        }
      });
    })
  },
  globalData: {
    userInfo: null,
    isRegisted: false,
    url: 'https://soulcode.cn',
    openid: '',
    isTeamed: false,
    pic: '',
    isAdmin: false,
    score: 0,
    data: {},
    teamPre:[]
  }
})